package com.it.iostream.Controller;


import com.it.iostream.Dao.ResPonseResult;
import com.it.iostream.Service.Impl.FileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


@RestController
@RequestMapping(value = "/nowfile")
public class FileUploadController {

    @Autowired
    private FileServiceImpl fileService;


    @RequestMapping(value = "/Index", method = RequestMethod.GET)
    public String Index() {
        return "/";
    }

    /*
    * 上传文件,请求参数为 file_upload
    * */
    @ResponseBody
    @RequestMapping(value = "/FileUpload")
    public ResPonseResult fileUpload(@NotNull @NotEmpty  @RequestParam(value = "file_upload")  MultipartFile file) throws Exception {
        ResPonseResult fileuploadresult = fileService.uploadfile(file);
        return fileuploadresult;
    }

    /*
     * 上传图片，请求参数为 img_upload
     * */
    @ResponseBody
    @RequestMapping(value = "/ImgUpload")
    public ResPonseResult ImgUpload(@NotNull @NotEmpty @RequestParam(value = "img_upload") MultipartFile file) throws Exception {
        ResPonseResult imageuploadresult = fileService.ImgUpload(file);
        return  imageuploadresult;
    }

    /*
     * 查询 findbyseq
     */
    @ResponseBody
    @RequestMapping(value = "{seq}",produces = MediaType.IMAGE_JPEG_VALUE)
    public  byte[] FindBySeq(@NotNull @NotEmpty @PathVariable String seq,HttpServletResponse response) throws Exception {
       return fileService.findbyseq(seq,response);
    }


}
